package org.web.ssm.service.wechatService.wechatServiceImpl;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.web.ssm.dao.ProductMapMapper;
import org.web.ssm.dao.ProductMapper;
import org.web.ssm.exception.BaseException;
import org.web.ssm.model.*;
import org.web.ssm.service.impl.OrderServiceImpl;
import org.web.ssm.service.wechatService.IProductMapService;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by LUOLIANG on 2016/6/22.
 */
@Service
public class ProductMapServiceImpl implements IProductMapService {
    private Logger logger = LogManager.getLogger(OrderServiceImpl.class);
    @Autowired
    private ProductMapMapper productMapMapper;
    @Autowired
    private ProductMapper productMapper;

    @Override
    public void insertOrUpdateProductMapping(User user, String productid, String money) {
        List<ProductMap> list = productMapMapper.selectByUser(user.getUserid(), productid);

        /**
         * 如果为空说明是第一次购买该产品
         */
        ProductMap productMap;
        if (list.isEmpty()) {
            Product product = productMapper.selectByPrimarykey(productid);
            productMap = new ProductMap(Double.valueOf(money), new Date(), new Date(), product, user);
            productMapMapper.insertProductMap(productMap);
            //将钱修改为本金+当日收益
            productMap.setMoney(productMapMapper.selectByUserproduct(user.getUserid(), productid).getProfit() + Double.valueOf(money));
            System.out.println(productMap);
        } else {
            productMap = list.get(0);
            //不为空说明是已经购买该产品，想买入更多，只用更新钱数
            productMap.setMoney(productMap.getMoney() + Double.valueOf(money));
        }
        try {
            int res = productMapMapper.updateProductMap(productMap);
            if (res != 1) {
                System.out.println("更新失败");
            }
        } catch (BaseException e) {
            throw e;
        }

    }

    @Override
    public ProductMapView getStatistics(String userid) throws ParseException {
        //查询用户买的所有理财产品
        List<ProductMap> list = productMapMapper.selectByUser(userid, "");

//        System.out.println(list);
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        for (int i = 0; i < list.size(); i++) {
            ProductMap productmap = list.get(i);
//            System.out.println(df.format(productmap.getEndtime()));
//            System.out.println(df.format(new Date()));
            if (!df.parse(df.format(productmap.getEndtime())).before(df.parse(df.format(new Date())))) {
                System.out.println("这是今天的订单");
            } else {
                System.out.println("这是昨天的的订单");
                UserProductInfoView userProductInfoView = productMapMapper.selectByUserproduct(userid, String.valueOf(productmap.getProduct().getId()));

                //更新本金=本金+收益
                productmap.setMoney(productmap.getMoney() + userProductInfoView.getProfit());
                productMapMapper.updateProductMap(productmap);
            }

        }

        return productMapMapper.selectViewStatis(userid);
    }

//    public static Date getNextDay(Date date) {
//        Calendar calendar = Calendar.getInstance();
//        calendar.setTime(date);
//        calendar.add(Calendar.DAY_OF_MONTH, -1);
//        date = calendar.getTime();
//
//        return date;
//    }

}
